<?php
declare (strict_types=1);

namespace app\common\traits;

/**
 * 数据仓库公共方法
 * Trait RepositoryTrait
 * @package app\traits
 */
trait RepositoryTrait
{
    /**
     * @param $id
     * @param array $field
     * @param bool $where
     * @return mixed
     */
    public static function getInfoByID($id, $field = [], $where = [])
    {
        return self::$model::where([self::$pk => $id])->where($where)->field($field ?: self::$model::$showField)->find();
    }
    /**
     * 通过name获取信息
     */
    public static function getInfoByName($name, $field = [], $where = [])
    {
        return self::$model::where([self::$name => $name])->where($where)->field($field ?: self::$model::$showField)->find();
    }

    /**
     * 插入数据
     * @param array $data 插入的数据
     * @param bool $getLastInsID 是否返回最新插入记录ID
     * @param bool $replace 使用replace替换
     * @return mixed
     */
    public static function insertData(array $data = [],bool $getLastInsID = false,bool $replace = false){
        if($replace){
            $m = new self::$model();
            $res = $m->replace()->insert($data);
        }else{
            $res = self::$model::insert($data, $getLastInsID);
        }
        return $res;
    }

    /**
     * 插入数据,忽略错误
     * @param array $data 插入的数据
     * @param bool $getLastInsID 是否返回最新插入记录ID
     * @return mixed
     */
    public static function insertIgnore(array $data = [],bool $getLastInsID = false){
        $model = new self::$model();
        return $model->extra('IGNORE')->insert($data,$getLastInsID);
    }

    /**
     * 批量插入数据
     * @param array $data 批量插入数据
     * @param int $limit
     * @param bool $replace 使用replace替换
     * @return mixed
     */
    public static function insertAllData(array $data = [], int $limit = 0, bool $replace = false){
        if($replace){
            $m = new self::$model();
            $res = $m->replace()->saveAll($data);
        }else{
            $res = self::$model::insertAll($data, $limit);
        }
        return $res;
    }

    /**
     * 更新数据
     * @param array $data 更新的数据
     * @param mixed $where 更新条件
     * @return bool
     */
    public static function updateData(array $data = [],$where = []){
        return self::$model::where($where)->save($data);
    }
    /**
     * 插入数据
     */
    public static function insert($data){
        return self::$model::create($data);
    }
    /**
     * 查询单条数据
     * @param mixed $where 查询条件
     * @return mixed
     */
    public static function getOneData($where, $field = ['*']){
        return self::$model::where($where)->field($field)->find();
    }

    /**
     * 查询最大字段
     */
    public static function getMax($where=[],$field){
        return self::$model::where($where)->max($field);
    }
    /**
     * 查询多条数据
     * @param $where mixed 查询条件
     * @param array $order 排序
     * @param bool $all 是否查全部
     * @param int $offset 分页起始
     * @param int $size 分页长度
     * @return mixed
     */
    public static function getData($where,array $order = [], bool $all = false, int $offset = 0,int $size = 10, array $field = ['*']){
        switch(count($order)) {
            case 0:
                $order = ['',''];
                break;
            case 1:
                $order[1] = '';
                break;
        }
        if($all){
            $data = self::$model::where($where)->field($field)->order($order[0], $order[1])->select();
        }else{
            $data = self::$model::where($where)->field($field)->order($order[0],$order[1])->limit($offset,$size)->select();
        }
        return $data;
    }
    /**
     * 查询条目数
     */
    public static function count($where = []){
        $num = self::$model::where($where)->count();
        return $num;
    }
    /**
     * 分页
     */
    public static function page(int $page,int $pageSize,$where,$order=[]){
        if(empty($order)) {
            $list = self::$model::where($where)->paginate([
                'page' => $page,
                'list_rows' => $pageSize
            ]);
        }else{
            $list = self::$model::where($where)->order($order)->paginate([
                'page' => $page,
                'list_rows' => $pageSize
            ]);
        }
        return $list;
    }
    /**
     * 真实删除数据库数据
     * @param mixed $where 删除条件
     * @return mixed
     */
    public static function realDelete($where){
        return self::$model::where($where)->delete();
    }

    /**
     * 模拟删除数据
     * @param mixed $where 删除条件
     * @param int $time 是否记录时间
     * @return mixed
     */
    public static function simulateDelete($where,$time=0){
        $save = ['is_del' => '1'];
        if($time){
            $save['del_time'] = date('Y-m-d H:i:s');
        }
        return self::$model::where($where)->save($save);
    }

    /**
     * 事务开启
     * @return mixed
     */
    public static function startTrans(){
        return self::$model::startTrans();
    }

    /**
     * 事务提交
     * @return mixed
     */
    public static function commit(){
        return self::$model::commit();
    }

    /**
     * 事务回滚
     * @return mixed
     */
    public static function rollback(){
        return self::$model::rollback();
    }

    /**
     * 查询指定字段的内容
     * @param string $name 查询字段
     * @param mixed $where 查询条件
     * @return array
     */
    public static function getFieldContents(string $name,$where="",$limit = []){
        $contents = [];
        if(!empty($limit)){
            $data = self::$model::where($where)->field($name)->limit($limit[0],$limit[1])->select();
        }else{
            $data = self::$model::where($where)->field($name)->select();
        }
        $arr = ObjectToArray($data);
        foreach ($arr as $key=>$value){
            $contents[] = $value[$name];
        }
        return $contents;
    }

    /**
     * 更新数据 自增一个字段的值
     * @param array $data 更新的数据
     * @param mixed $where 更新条件
     * @return bool
     */
    public static function updateInc(string $data = '', float $num = 1, $where = []){
        return self::$model::where($where)->inc($data, $num)->update();
    }

    /**
     * 更新数据 自减一个字段的值
     * @param array $data 更新的数据
     * @param mixed $where 更新条件
     * @return bool
     */
    public static function updateDec(string $data = '', float $num = 1, $where = []){
        return self::$model::where($where)->dec($data, $num)->update();
    }

    /**
     * 查询数据in为条件
     * @param array $name 字段名
     * @param array $data 条件
     * @param mixed $field 查询字段
     * @return bool
     */
    public static function getDataIn(string $name = '', string $data = '', $field = ['*']){
        return self::$model::whereIn($name, $data)->field($field)->select();
    }

    /**
     * 查询数据notin为条件
     * @param array $name 字段名
     * @param array $data 条件
     * @param mixed $field 查询字段
     * @return bool
     */
    public static function getDataNotIn(string $name = '', string $data = '', $field = ['*']){
        return self::$model::whereNotIn($name, $data)->field($field)->select();
    }

    /**
     * 插入数据duplicate
     * @param array $data 插入的数据
     * @param bool $getLastInsID 是否返回最新插入记录ID
     * @param bool $replace 使用replace替换
     * @return mixed
     */
    public static function insertDuplicateData(array $data = [], array $updateData = []){
        return self::$model::duplicate($updateData)->insert($data);
    }
}